package cn.smile.spring;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
import java.time.Duration;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 13:05 2019/4/25
 */
@Configuration
public class RedisConfig {

    /**
     * redis连接池配置
     *
     * @return
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //最大池
        jedisPoolConfig.setMaxTotal(800);
        //空闲连接
        jedisPoolConfig.setMaxIdle(100);
        jedisPoolConfig.setMaxWaitMillis(3000);
        jedisPoolConfig.setTestOnBorrow(true);
        return jedisPoolConfig;
    }



    /**
     * Redis链接工厂,哨兵模式
     *
     * @return
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        //该配置为哨兵
        //JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration(),jedisPoolConfig());

        //单机
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
        jpcb.poolConfig(jedisPoolConfig());
        JedisClientConfiguration jedisClientConfiguration = jpcb.build();
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration(),jedisClientConfiguration);
        return jedisConnectionFactory;
    }


    /**
     * redisTemplate
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(){
        RedisTemplate redisTemplate=new RedisTemplate();
        StringRedisSerializer serializer=new StringRedisSerializer();
        //key序列化方式
        redisTemplate.setKeySerializer(serializer);
        //value序列化方式
        redisTemplate.setValueSerializer(serializer);
        //加入连接工厂
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        //启用事务支持，必须配合@Transactional一起使用，否则连接不会close
        redisTemplate.setEnableTransactionSupport(true);
        return redisTemplate;
    }

    /**
     * 哨兵模式配置
     * @return
     */
    //@Bean
    public RedisSentinelConfiguration sentinelConfiguration(){
        //哨兵配置
        RedisSentinelConfiguration configuration=new RedisSentinelConfiguration();
        //哨兵的名字
        configuration.setMaster("Master");
        //多个哨兵配置，这里只有一个哨兵
        Set<RedisNode> redisNodes = new HashSet<RedisNode>();
        redisNodes.add(new RedisNode("47.105.164.51",26379));
        redisNodes.add(new RedisNode("47.105.164.51",36379));
        //添加
        configuration.setSentinels(redisNodes);
        configuration.setPassword("MaoSuyu123123");
        return configuration;
    }

    /**
     * 单机模式配置
     * @return
     */
    public RedisStandaloneConfiguration redisStandaloneConfiguration(){
        RedisStandaloneConfiguration redisStandaloneConfiguration=new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("175.24.228.237");
        redisStandaloneConfiguration.setPassword("maomao");
        redisStandaloneConfiguration.setPort(6379);
        return redisStandaloneConfiguration;
    }



}